package com.ziyucoding.springboot.labs.lab8.skywalking.mysqldemo.controller;

import com.ziyucoding.springboot.labs.lab8.skywalking.mysqldemo.dataobject.UserDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 示例 控制器
 *
 * @author ziyucoding
 * @date 2022-10-31
 **/
@RestController
@RequestMapping("/demo")
public class DemoController {

    @Autowired
    private JdbcTemplate template;

    @GetMapping("/mysql")
    public Map<String, Object> mysql() {
        UserDO userDO = this.selectById(1);

        Map<String, Object> result = new HashMap<>();
        result.put("code", 0);
        result.put("msg", "成功");

        Map<String, Object> data = new HashMap<>();
        data.put("id", userDO.getId());
        data.put("userName", userDO.getUserName());

        result.put("data", data);
        return result;
    }

    private UserDO selectById(Integer id) {
        return template.queryForObject("SELECT id, user_name, pwd FROM t_user WHERE id = ?",
                new BeanPropertyRowMapper<>(UserDO.class),
                id);
    }
}
